package com.hcss.core.common.permissions.service;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hcss.core.annotation.SysOpertionLog;
import com.hcss.core.common.logsmgt.dao.LoginLogDao;
import com.hcss.core.common.logsmgt.model.LoginLog;
import com.hcss.core.common.permissions.dao.UserInfoDao;
import com.hcss.core.common.permissions.model.UserInfo;
import com.hcss.core.mvc.BaseService;
import com.hcss.core.mvc.page.Pager;
import com.hcss.core.util.CommonUtil;
import com.hcss.core.util.DesEncrypt;
import com.hcss.core.util.StringUtils;
import com.hcss.core.util.SysDictionary;


/**
 * <p>用户信息管理Service</p>
 * 
 * @author lvf
 * @version 1.0
 * @since 1.0
 */
@Service("userInfoService")
public class UserInfoService extends BaseService{
	
	@Autowired
	private UserInfoDao dao;
	@Autowired
	private LoginLogDao loginLogDao;
	/**
	 * 保存用户
	 * @param user 用户
	 * @param userRoleRels 角色管理
	 */
	@SysOpertionLog(opModel ="User",opFunction="Create User!",opMark="")
	public int addUserInfo(UserInfo user,String userRoleRels)  throws Exception  {
		int id = dao.addUserInfo(user);
//		if(StringUtils.isNotEmpty(userRoleRels)){
//			String roles[] = userRoleRels.split(",");
//			for (int i = 0; i < roles.length; i++) {
//				UserRoleRelInfo relRole = new UserRoleRelInfo();
//				relRole.setRoleId(roles[i]);
//				relRole.setUserId(String.valueOf(id));
//				userRoleRelManageDao.addUserRoleRelInfo(relRole);
//			}
//		}
		return id;
	}
	
	/** 
	* <p> editUserInfo TODO修改用户</p>
	* @param user
	* @param userRoleRels
	* @return
	* @throws Exception  int 设定文件 
	*/
	@SysOpertionLog(opModel ="User",opFunction="Edit User!",opMark="")
	public int editUserInfo(UserInfo user,String userRoleRels) throws Exception{
		int state = dao.updateUserInfo(user);
//		if(StringUtils.isNotEmpty(userRoleRels)){
			//userRoleRelManageDao.deleteUserRoleRelInfo(String.valueOf(user.getId()));
//			String roles[] = userRoleRels.split(",");
//			for (int i = 0; i < roles.length; i++) {
//				UserRoleRelInfo relRole = new UserRoleRelInfo();
//				relRole.setRoleId(roles[i]);
//				relRole.setUserId(String.valueOf(user.getId()));
//				userRoleRelManageDao.addUserRoleRelInfo(relRole);
//			}
//		}
		return state;
	}
	/**
	 * 查询所有用户
	 * @param name 名称
	 * @param unlock 是否锁定
	 * @return
	 */
	public Pager getAllUsers(Pager page,UserInfo user) throws Exception {
		return dao.getAllUsers(page,user);
	}
	
	/**
	 * 用户解锁
	 * @param ids 解锁ids
	 * @return
	 */
	@SysOpertionLog(opModel ="User",opFunction="Unlock User!",opMark="")
	public int unlock(String ids) throws Exception {
		return dao.unlock(ids)>0?1:0;
	}
	
	/**
	 * 用户锁定
	 * @param ids 解锁ids
	 * @return
	 */
	@SysOpertionLog(opModel ="User",opFunction="lock User!",opMark="")
	public int lock(String ids) throws Exception {
		return dao.lock(ids)>0?1:0;
	}
	
	/**
	 * 密码重置
	 * @param ids 用户ids
	 * @return
	 */
	@SysOpertionLog(opModel ="User",opFunction="reset password!",opMark="")
	public int executeResetPwd(String ids) throws Exception{
		if(StringUtils.isNotEmpty(ids)){
			String idArray[] = ids.split(",");
			for (int i = 0; i < idArray.length; i++) {
				UserInfo user = new UserInfo();
				user.setId(Integer.parseInt(idArray[i]));
				user.setUserPwd(SysDictionary.FRIST_PWD);
				dao.resetUserPwd(user);
			}
		}
		return 1;
	}
	
	
	/** 
	* <p> findById TODO 获取 用户 信息</p>
	* @param userId
	* @return  UserInfo 设定文件 
	 * @throws Exception 
	*/
	public UserInfo findById(String userId) throws Exception{
		UserInfo user = dao.findById(userId);
		return user;
	}
	
	/** 
	* <p> deleteUser TODO 删除用户</p>
	* @param ids
	* @return
	* @throws Exception  int 设定文件 
	*/
	@SysOpertionLog(opModel ="User",opFunction="delete user!",opMark="")
	public int deleteUser(String ids) throws Exception{
		return dao.deleteUserInfo(ids);
	}
    
    /**
     * @Title: findUserNameIsExist
     * <p>验证用户名是否存在<p>
     * @param userName
     * @param userId
     * @return
     * @return: int
     */
    public int findUserNameIsExist(String userName,int userId){
    	return dao.findUserNameIsExist(userName, userId);
    }
    /**
	 * 查询用户详细信息
	 * @param userInfo
	 * @return
	 * @throws Exception
	 */
	public UserInfo getLoginUserInfos(UserInfo userInfo) throws Exception {
		UserInfo user = dao.getUserInfoByuserNoAndpwd(userInfo);
		return user;
	}
    /**
     * 根据id获取用户信息
     * 
     * @param id
     * @return UserInfo
     * @throws Exception
     */
    public UserInfo getUserInfoById(String id) throws Exception {
    	return dao.getUserInfoById(id);
    }
	/**
	 * 验证登录用户名密码
	 * @param userInfo
	 * @return
	 * @throws Exception
	 */
	public int Login(UserInfo userInfo) throws Exception{
		DesEncrypt des = new DesEncrypt();
		String pwd = des.encrypt(userInfo.getUserPwd());
		userInfo.setUserPwd(pwd);
		return dao.queryUserId(userInfo);
	}
	/** 
	* <p> saveLog TODO 增加登陆日志</p>
	* @param request
	* @param loginType  void 设定文件 
	*/
	public void saveLog(HttpServletRequest request,String loginType){
		LoginLog loginlog = new LoginLog();
		loginlog.setUserId(this.getUserInfo().getId());
		loginlog.setLoginIp(CommonUtil.getRemortAddr(request));
		loginlog.setLoginTime(new Date());
		loginlog.setLoginType(loginType);
		loginLogDao.addLoginLog(loginlog);
	}
}